<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="部署规则名称" prop="dname">
      <el-input v-model="dataForm.dname" placeholder="部署规则名称"></el-input>
    </el-form-item>
    <el-form-item label="操作系统类型" prop="osType">
      <el-input v-model="dataForm.osType" placeholder="操作系统类型"></el-input>
    </el-form-item>
    <el-form-item label="CPU核数" prop="cpu">
      <el-input v-model="dataForm.cpu" placeholder="CPU核数"></el-input>
    </el-form-item>
    <el-form-item label="内存大小(G)" prop="memory">
      <el-input v-model="dataForm.memory" placeholder="内存大小(G)"></el-input>
    </el-form-item>
    <el-form-item label="磁盘空间(G)" prop="disk">
      <el-input v-model="dataForm.disk" placeholder="磁盘空间(G)"></el-input>
    </el-form-item>
    <el-form-item label="是否跨数据中心" prop="isCrossDc">
      <!--<el-input v-model="dataForm.isCrossDc" placeholder="是否跨数据中心"></el-input>-->
      <el-radio-group v-model="dataForm.isCrossDc">
        <el-radio :label="1">是</el-radio>
        <el-radio :label="0">否</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="数据中心ID" prop="dcId">
      <el-input v-model="dataForm.dcId" placeholder="数据中心ID"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          did: 0,
          dname: '',
          osType: '',
          cpu: '',
          memory: '',
          disk: '',
          isCrossDc: '',
          dcId: ''
        },
        dataRule: {
          dname: [
            { required: true, message: '部署规则名称不能为空', trigger: 'blur' }
          ],
          osType: [
            { required: true, message: '操作系统类型不能为空', trigger: 'blur' }
          ],
          cpu: [
            { required: true, message: 'CPU核数不能为空', trigger: 'blur' }
          ],
          memory: [
            { required: true, message: '内存大小(G)不能为空', trigger: 'blur' }
          ],
          disk: [
            { required: true, message: '磁盘空间(G)不能为空', trigger: 'blur' }
          ],
          isCrossDc: [
            { required: true, message: '是否跨数据中心不能为空', trigger: 'blur' }
          ],
          dcId: [
            { required: true, message: '数据中心ID不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.did = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.did) {
            this.$http({
              url: this.$http.adornHardWareUrl(`/generator/deploy/info/${this.dataForm.did}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.dname = data.deploy.dname
                this.dataForm.osType = data.deploy.osType
                this.dataForm.cpu = data.deploy.cpu
                this.dataForm.memory = data.deploy.memory
                this.dataForm.disk = data.deploy.disk
                this.dataForm.isCrossDc = data.deploy.isCrossDc
                this.dataForm.dcId = data.deploy.dcId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornHardWareUrl(`/generator/deploy/${!this.dataForm.did ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'did': this.dataForm.did || undefined,
                'dname': this.dataForm.dname,
                'osType': this.dataForm.osType,
                'cpu': this.dataForm.cpu,
                'memory': this.dataForm.memory,
                'disk': this.dataForm.disk,
                'isCrossDc': this.dataForm.isCrossDc,
                'dcId': this.dataForm.dcId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
